package com.lms.dao;


import com.lms.bean.RentRecord;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface RentRecordDao extends JpaRepository<RentRecord, Long>, JpaSpecificationExecutor<RentRecord> {
    @Query("select n from RentRecord n")
    List<RentRecord> getList(Pageable pageable);

    @Modifying
    @Transactional
    @Query("delete  from RentRecord n where n.rentId in ?1")
    void deleteAll(List<Long> ids);

    @Query("select count(n) from RentRecord n where n.rentId=?1")
    Long countByRentId(Long rentId);

    @Query("select n from RentRecord n where n.rentId=?1")
    List<RentRecord> findByRentId(Long rentId, Pageable pageable);

    //徐聪添加的方法，用于查询某用户可还书籍数量
    @Query("select count(n) from RentRecord n where n.user.userId = ?1 and n.book.bookName like ?2")
    Long countByUserIdAndAndBookName(Long userId, String bookname);
}
